﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace UI.Dendrogram
{
    public class HierarchicalData
    {
        public int ID { get; set; }
        public string Label { get; set; }
        public double Distance { get; set; }
        public object Tag { get; set; }
        public HierarchicalData LeftChild { get; set; }
        public HierarchicalData RightChild { get; set; }

        public HierarchicalData(int id, string label, object tag, double distance)
        {
            this.ID = id;
            this.Label = label;
            this.Tag = tag;
            this.Distance = distance;
            this.LeftChild = null;
            this.RightChild = null;
        }

        public static HierarchicalData ConvertFromBinaryData(BinaryHierarchicalData treeData)
        {
            HierarchicalData data=new HierarchicalData(treeData.ID,treeData.Label,treeData.Tag,treeData.Distance);
            if(treeData.LeftChild!=null)
                AddChild(data,treeData.LeftChild,true);
            else if(treeData.RightChild!=null)
                AddChild(data, treeData.LeftChild, false);
            return data;
        }

        private static void AddChild(
            HierarchicalData parent, 
            BinaryHierarchicalData branch,
            bool isLeft)
        {
            HierarchicalData child = new HierarchicalData(branch.ID, branch.Label, branch.Tag, branch.Distance);
            if(isLeft)
            {
                parent.LeftChild = child;
            }
            else
            {
                parent.RightChild = child;
            }
            if (branch.LeftChild != null)
                AddChild(child, branch.LeftChild, true);
            else if(branch.RightChild!=null)
                AddChild(child, branch.LeftChild, false);
        }
    }
}
